using ERP.Domain.Team.ProductionSystem;
using ERP.Team.HumanResources;
using ERP.Team.OrderManagement;
using ERP.Team.ProductionSystem;
using ERP.Team.RBAC;
using ERP.Team.WarehouseManagement;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace ERP.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class ERPDbContext :
    AbpDbContext<ERPDbContext>,
    ITenantManagementDbContext,
    IIdentityDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */


    #region Entities from the modules

    /* Notice: We only implemented IIdentityProDbContext and ISaasDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityProDbContext and ISaasDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    // Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }


    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    #endregion

    public ERPDbContext(DbContextOptions<ERPDbContext> options)
        : base(options)
    {

    }
    //权限表-grx
    public DbSet<PermissionModel> Permissions { get; set; }
    //用户表-xzc
    public DbSet<UserModel> User { get; set; }
    //角色表-zyn
    public DbSet<RoleModel> Role { get; set; }
    //面试管理--bpy
    public DbSet<InterviewMenagementModel> InterviewMenagementModel { get; set; }
    //部门管理-bpy
    public DbSet<DepartmentModel> DepartmentModel { get; set; }
    /// <summary>
    /// 角色权限--bpy
    /// </summary>
    public DbSet<RolePermissionModel> RolePermissionModel { get; set; }
    /// <summary>
    /// 用户角色--bpy
    /// </summary>
    public DbSet<UserRoleModel> UserRoleModel { get; set; }
    /// <summary>
    /// 职级管理--zyn
    /// </summary>
    public DbSet<JobGradeManagement> JobGradeManagement { get; set; }
    /// <summary>
    /// 人资系统-入职审核和入职申请--xzc
    /// </summary>
    public DbSet<OnboardingReview> OnboardingReview { get; set; }
    /// <summary>
    /// 生产系统-生产管理-生产报工-xzc
    /// </summary>
    public DbSet<ProductionReportingMain> ProductionReportingMain { get; set; }
    /// <summary>
    /// 生产系统-设备管理-维修单-xzc
    /// </summary>
    public DbSet<Repair> Repair { get; set; }
    /// <summary>
    /// 物料分类
    /// </summary>
    public DbSet<MaterialType> MaterialType { get; set; }
    /// <summary>
    /// 生产工单主表-zyn
    /// </summary>
    public DbSet<ProductionWorkOrderMain> ProductionWorkOrderMain { get; set; }
    /// <summary>
    /// 生产系统-质量管理-出货检验-xzc
    /// </summary>
    public DbSet<OutgoingInspection> OutgoingInspection { get; set; }
    /// <summary>
    /// 仓储管理-库存信息-库存现有量-xzc
    /// </summary>
    public DbSet<Inventory> Inventory { get; set; }
    /// <summary>
    /// 仓储管理-销售退货-xzc
    /// </summary>
    public DbSet<SalesReturns> SalesReturns { get; set; }
    /// <summary>
    /// 仓储管理-外协发料-xzc
    /// </summary>
    public DbSet<IssuingMaterials> IssuingMaterials { get; set; }
    /// <summary>
    /// 客户管理--bpy
    /// </summary>
    public DbSet<CustomerModel> CustomerModel { get; set; }
    /// <summary>
    /// 生产系统--生产任务--xzc
    /// </summary>
    public DbSet<ProductionTask> ProductionTask { get; set; }

    /// <summary>
    /// 生产系统-主数据-供应商管理--wyx
    /// </summary>
    public DbSet<Supplier> Supplier { get; set; }
    /// <summary>
    /// 生产系统-主数据-工序管理--wyx
    /// </summary>
    public DbSet<Procedure> Procedure { get; set; }
    /// <summary>
    /// 生产系统-主数据-工艺流程--wyx
    /// </summary>
    public DbSet<Workmanship> Workmanship { get; set; }
    /// <summary>
    /// 生产系统-主数据-车间设置--wyx
    /// </summary>
    public DbSet<Workshop> Workshop { get; set; }
    /// <summary>
    /// 生产系统-主数据-生产废料--wyx
    /// </summary>
    public DbSet<ProductionWasteOrder> ProductionWasteOrder { get; set; }
    /// <summary>
    /// 生产系统-主数据-设备类型设置--wyx
    /// </summary>
    public DbSet<DeviceType> DeviceType { get; set; }
    /// <summary>
    /// 人才登记表--grx
    /// </summary>
    public DbSet<TalentregistrationModel> TalentregistrationModel { get; set; }
    /// <summary>
    /// 生产派单主表--zyn
    /// </summary>
    public DbSet<ProductionDispatchMain> ProductionDispatchMain { get; set; }

    /// <summary>
    /// 客户订单--
    /// 客户订单--bpy
    /// </summary>
    public DbSet<CustomerOrderModel> CustomerOrderModel { get; set; }
    /// <summary>
    /// 产品关联表
    /// </summary>
    public DbSet<ProductInfoRelationModel> ProductInfoRelationModel { get; set; }
    /// <summary>
    /// BOM物料表--GRX
    /// </summary>
    public DbSet<BOMModel> BOMModel { get; set; }
    /// <summary>
    /// 物料管理--GRX
    /// </summary>
    public DbSet<MaterialproductModel> MaterialproductModel { get; set; }
    /// <summary>
    /// 生产退料主表--zyn
    /// </summary>
    public DbSet<ProductionMaterialReturn> ProductionMaterialReturn { get; set; }
    /// <summary>
    /// 生产系统-生产管理-生产领料--grx
    /// </summary>
    public DbSet<ProductionpickingModel> ProductionpickingModel { get; set; }

    /// <summary>
    /// 物料需求--bpy
    /// </summary>
    public DbSet<MaterialRequirementModel> MaterialRequirementModel { get; set; }
    /// <summary>
    /// 生产系统-设备管理-设备台账--grx
    /// </summary>
    public DbSet<DeviceledgerModel> DeviceledgerModel { get; set; }
    /// <summary>
    /// 过程检验-zyn
    /// </summary>
    public DbSet<Process> Process { get; set; }

    /// <summary>
    /// 生产排产
    /// </summary>
    public DbSet<ProductionSchedulingModel> ProductionSchedulingModel { get; set; }
    /// <summary>
    /// 生产系统-质量管理-待检任务--grx
    /// </summary>
    public DbSet<PendingtasksModel> PendingtasksModel { get; set; }
    /// <summary>
    /// 生产系统-点检保养项目-xzc
    /// </summary>
    public DbSet<SpotChecks> SpotChecks { get; set; }
    /// <summary>
    /// 生产系统-排班管理-排班计划--grx
    /// </summary>
    public DbSet<ShiftplanningModel> ShiftplanningModel { get; set; }
    /// <summary>
    /// 来料检验实体--bpy
    /// </summary>
    public DbSet<IncomingInspection> IncomingInspection { get; set; }
    /// <summary>
    /// 节假日设置--zyn
    /// </summary>
    public DbSet<HolidaysSet> HolidaysSet { get; set; }
    /// <summary>
    /// 班次实体类--grx
    /// </summary>
    public DbSet<ShiftModel> ShiftModel { get; set; }
    /// <summary>
    /// 班组实体类--grx
    /// </summary>
    public DbSet<TeamModel> TeamModel { get; set; }
        /// <summary>
    /// 供应商退货管理--bpy
    /// </summary>
    public DbSet<SupplierReturns> SupplierReturns { get; set; }
    /// <summary>
    /// 生产系统-设备管理-点检保养计划-xzc
    /// </summary>
    public DbSet<Maintenance> Maintenance { get; set; }
    /// <summary>
    /// 仓储管理-库存盘点-xzc
    /// </summary>
    public DbSet<InventoryCounts> InventoryCounts { get; set; }
    /// <summary>
    /// 仓储管理-采购入库-grx
    /// </summary>
    public DbSet<ProcurementwarehousingModel> ProcurementwarehousingModel { get; set; }
    /// <summary>
    /// 生产系统-班组设置-zyn
    /// </summary>
    public DbSet<TeamSet> TeamSet { get; set; }
    /// <summary>
    /// 装箱管理--bpy
    /// </summary>
    public DbSet<PackingModel> PackingModel { get; set; }
    /// <summary>
    /// 销售订单主表--bpy
    /// </summary>
    public DbSet<SalesOrderMainModel> SalesOrderMainModel { get; set; }
    /// <summary>
    /// 销售订单明细--bpy
    /// </summary>
    public DbSet<SalesOrderProductDetailModel> SalesOrderProductDetailModel { get; set; }
    /// <summary>
    /// 仓储管理-赋码管理-grx
    /// </summary>
    public DbSet<CodingManagementModel> CodingManagementModel { get; set; }
    /// <summary>
    /// 销售出库-zyn
    /// </summary>
    public DbSet<ShiftSchedulingCalendar> ShiftSchedulingCalendar { get; set; }
    /// <summary>
    /// 外协入库-zyn
    /// </summary>
    public DbSet<IssuingStore> IssuingStore { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureFeatureManagement();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureTenantManagement();
        builder.ConfigureBlobStoring();

        /* Configure your own tables/entities inside here */

        // 配置SupplierReturns实体
        builder.Entity<SupplierReturns>(b =>
        {
            b.Property(e => e.ConcurrencyStamp).IsConcurrencyToken();
        });

        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(ERPConsts.DbTablePrefix + "YourEntities", ERPConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});
    }
}
